package com.sgb.kjwl.view.widget;

import android.os.CountDownTimer;

/**
 * Describe:
 * 时间倒计时工具
 * 支持天时分秒
 * Author:  yangLei
 * Date:    2021/4/14
 */
public class TimeCountDown extends CountDownTimer {

    private CountdownCallBack countdownCallBack;

    public TimeCountDown(long millisInFuture, long countDownInterval) {
        /*
         * 参数依次为总时长,和计时的时间间隔
         */
        super(millisInFuture, countDownInterval);
    }


    public void setCountdownCallBack(CountdownCallBack countdownCallBack) {
        this.countdownCallBack = countdownCallBack;
    }

    @Override
    public void onFinish() {
        /*
         * 计时完毕时触发
         */
        if (countdownCallBack != null)
            countdownCallBack.countdown(0, 0, 0, 0);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        /*
         * 计时过程显示
         */
        long millis = millisUntilFinished / 1000;
        int second = (int) (millis % 60);
        millis /= 60;
        int minute = (int) (millis % 60);
        millis /= 60;
        int hour = (int) (millis % 24);
        millis /= 24;
        int day = (int) millis;
        if (countdownCallBack != null){
            countdownCallBack.countdown(day, hour, minute, second);}
    }

    public interface CountdownCallBack {
        void countdown(int days, int hours, int minutes, int seconds);
    }
}
